package com.cicadamob.contact.api.service;

import java.io.File;

import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;

/**
 * sd卡工具类
 * 
 * @author Administrator
 * 
 */
public class SDcardUtil {
	/**
	 * 是否有sd卡
	 * @return
	 */
	public static boolean sdCardIsOk() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return true;
		}
		return false;
	}
/**
 * sd卡是否是只读
 * @return
 */
	public static boolean sdCardisReadOnly() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED_READ_ONLY)) {
			return true;
		}
		return false;
	}
	/**
	 * sd卡空间是否充足
	 * @return
	 */
	public static boolean sdSizeAvailable(){
		File pathFile = Environment.getExternalStorageDirectory();
		StatFs statfs = new StatFs(pathFile.getPath());
		//获得可供程序使用的Block数量
		long nAvailaBlock = statfs.getAvailableBlocks();
		//获得SDCard上每个block的SIZE
		long nBlocSize = statfs.getBlockSize();
		//计算SDCard剩余大小MB
		long nSDFreeSize = nAvailaBlock * nBlocSize / 1024 / 1024;
		if(nSDFreeSize>15){
			return true;
		}
		return false;
	}
	/**
	 * 手机内存是否充足
	 * @return
	 */
   public static boolean memoryAvailable(Context context){
	   File path = Environment.getDataDirectory();
	   StatFs stat = new StatFs(path.getPath());
	   long blockSize = stat.getBlockSize();
	   long availableBlocks = stat.getAvailableBlocks();
	   // 手机内存剩余容量
	   long nSDFreeSize = availableBlocks * blockSize / 1024 / 1024;
	  // String memoryAvail = formatSize(context,availableBlocks * blockSize);
	   if(nSDFreeSize>15){
		   return true;
	   } 
	  return false;

   }
   
 
   private static String formatSize(Context context,long size) {
   return Formatter.formatFileSize(context, size);
   }
}
